package han.parser;

import han.entity.FinanceProjectIdEntity;
import han.entity.FinanceProjectLabelEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.sql.Timestamp;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @author: Han
 * @date: 2018/7/5 13:54
 * @dscription: TODO
 */
public class FinanceProjectIdParser {

    /**
     * 解析json字符串,获取公司id
     *
     * @param rowJsonStr json字符串
     */
    public static Set<FinanceProjectIdEntity> getProjectId(String rowJsonStr) {
        JSONObject pageDataJson = getPageDataJsonObj(rowJsonStr);
        JSONArray dataArray = pageDataJson.getJSONArray("data");
        Set<FinanceProjectIdEntity> idSet = new LinkedHashSet<>();
        for (int i = 0; i < dataArray.length(); i++) {
            JSONObject tempJsonObj = dataArray.getJSONObject(i);
            int krId = tempJsonObj.getInt("id");
            int cityId = tempJsonObj.getInt("city");
            FinanceProjectIdEntity entity = new FinanceProjectIdEntity();
            entity.setProjectId(krId + "");
            long time = System.currentTimeMillis();
            Timestamp timestamp = new Timestamp(time);
            entity.setAddTime(timestamp);
            entity.setLastRequestTime(timestamp);
            entity.setDeleteFlag(0);
            entity.setTimes(0);
            entity.setCity(cityId + "");
            idSet.add(entity);
        }
        return idSet;
    }

    /**
     * 获取总页数
     *
     * @param rowJsonStr 原始字符串
     * @return 总页数
     */
    public static int getTotalPages(String rowJsonStr) {
        JSONObject pageDataJsonObj = getPageDataJsonObj(rowJsonStr);
        int totalPages = pageDataJsonObj.getInt("totalPages");
        //如果大于等于50,只取50页
        totalPages = totalPages >= 50 ? 50 : totalPages;
        return totalPages;
    }

    /**
     * 获取PageData的json字符串
     *
     * @param rowJsonStr 原始json字符串
     * @return JSONObject
     */
    private static JSONObject getPageDataJsonObj(String rowJsonStr) {
        rowJsonStr = rowJsonStr.replaceAll("\uFEFF", "");
        JSONObject rowJsonObj = new JSONObject(rowJsonStr);
        JSONObject dataJsonObj = rowJsonObj.getJSONObject("data");
        return dataJsonObj.getJSONObject("pageData");
    }

    /**
     * 从手机链接返回的json字符串中解析出项目id
     *
     * @param str json字符串
     * @return 解析后的id
     */
    public static Set<FinanceProjectIdEntity> getProjectIdM(String str, FinanceProjectLabelEntity labelEntity) {
        Set<FinanceProjectIdEntity> idEntitySet = new HashSet<>();

        JSONObject dataObj = new JSONObject(str).getJSONObject("data");
        //获取总页数
        int totalPages = dataObj.getInt("totalPages");
        labelEntity.setTotalPages(totalPages);
        //获取其余值
        JSONArray dataArray = dataObj.getJSONArray("data");
        for (int index = 0; index < dataArray.length(); index++) {
            JSONObject tempJsonObj = dataArray.getJSONObject(index);
            String projectId = tempJsonObj.getLong("ccid") + "";
            String city = "";
            try {
                city = tempJsonObj.getString("city");
            } catch (JSONException ignore) {
            }
            FinanceProjectIdEntity entity = new FinanceProjectIdEntity();
            entity.setProjectId(projectId);
            long time = System.currentTimeMillis();
            Timestamp timestamp = new Timestamp(time);
            entity.setAddTime(timestamp);
            entity.setLastRequestTime(timestamp);
            entity.setDeleteFlag(0);
            entity.setTimes(0);
            entity.setCity(city);
            entity.setLabelId(labelEntity.getId());
            idEntitySet.add(entity);
        }

        return idEntitySet;
    }

    public static void main(String... args) {
        /*printlnInfo(getProjectIdM("{\"code\":0,\"data\":{\"totalPages\":481,\"pageSize\":20,\"page\":477," +
                "\"data\":[{\"brief\":\"医美整形仪器及服务提供商\",\"phase\":\"天使轮\",\"isFunding\":0,\"city\":\"深圳市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":5234,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"形容科技\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/com_logo/%E5%BD%A2%E5%AE%B9%E7%BD%91.jpg\"},{\"brief\":\"远距离射频识别产品研发商\",\"phase\":\"天使轮\",\"isFunding\":0,\"city\":\"广州市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":5203,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"天绎智能科技\",\"logo\":\"http://krplus-pic.b0.upaiyun.com/201712/06/467adb93c84667b61c7c6024d41e9882.png\"},{\"brief\":\"一款瘦身跟踪应用。\",\"phase\":\"未融资\",\"isFunding\":0,\"hotTrend\":0,\"iosAlexa\":146,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"健康健美\",\"weChatWeeklyReading\":0,\"ccid\":4983,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"瘦身看得见\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/201511/18/8c6399235f924a0e8ad7118df14f181ebae1.jpg\"},{\"brief\":\"专注糖尿病主题的搜索问答社区，旨在打造糖尿病患者互助问答平台\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"北京市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4981,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"糖必应\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/201511/18/8c4a17100e6d460b8a6138a7b44bc36a.jpg\"},{\"brief\":\"主打健康瘦身服务的网站及移动社区\",\"phase\":\"A轮\",\"isFunding\":0,\"city\":\"台湾\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4965,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"iFit爱瘦身\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/com_logo/iFit%E7%88%B1%E7%98%A6%E8%BA%AB.png\"},{\"brief\":\"医疗信息化系统研发商\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"北京市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4955,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"医科智谷\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/201511/18/b6a9dfa5471d44a48a11d095019ea4a7.jpg\"},{\"brief\":\"医患交流平台。\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"北京市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4945,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"闻康网\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/201511/18/2620a563afa84513a220dbe10b6643bb.jpg\"},{\"brief\":\"中医养生信息服务平台\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"北京市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":170,\"ccid\":4927,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"别有病网\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/201511/18/719a11360eb24212b8e7e952c3baed1f.jpg\"},{\"brief\":\"临床管理软件研发商\",\"phase\":\"新三板\",\"isFunding\":0,\"city\":\"江苏省\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4912,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"曼荼罗软件\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/201511/18/4ecdb9f41c6d47cb9fb4771f1f199247.jpg\"},{\"brief\":\"是一家集生物医药资讯、生物医药电子商务、网络技术服务于一体的生物医药商务平台，提供生物医药产品导购、\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"重庆市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4830,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"探生网\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/201511/18/489e72ed681743d39d3cf6da5b5ca8ba.jpg\"},{\"brief\":\"健康体检服务提供商\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"北京市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4820,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"天健阳光健康\",\"logo\":\"\"},{\"brief\":\"出国看病服务平台\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"河南省\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":true,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4774,\"hotIncrement\":0,\"bpUrl\":\"http://krplus-priv.b0.upaiyun.com/file/201704/17062850/出国医疗O2O平台商业计划书V4_2eld47t9u388cyf0.pdf\",\"name\":\"复诊网\",\"logo\":\"http://krplus-pic.b0.upaiyun.com/201805/22/73c2299e1f176e0c097e12f4bd181cf3.png\"},{\"brief\":\"医药零售电商平台\",\"phase\":\"并购\",\"isFunding\":0,\"city\":\"天津市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":6,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":513,\"ccid\":4711,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"金象网\",\"logo\":\"http://krplus-pic.b0.upaiyun.com/201805/18/272230dda03ff54b79c9de950588686f.png\"},{\"brief\":\"网上药店电商平台\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"广州市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4709,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"八百方\",\"logo\":\"http://krplus-pic.b0.upaiyun.com/201807/17/5148e31d2bf50ea02ef22efcc552cbee.png\"},{\"brief\":\"慢性疾病资讯平台\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"广东省\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4683,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"启康之路\",\"logo\":\"http://krplus-pic.b0.upaiyun.com/201808/02/9480259c97746166ee3233c113ca7182.png\"},{\"brief\":\"健康管理服务平台\",\"phase\":\"天使轮\",\"isFunding\":0,\"city\":\"上海市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4626,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"51健康网\",\"logo\":\"http://krplus-pic.b0.upaiyun.com/201806/01/10ff15e8ad9ed7eab76a626459486514.png\"},{\"brief\":\"致力于都市白领健康创意晚餐解决方案的企业\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"深圳市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4578,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"私厨盒子\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/201504/22151345/0b987247b1851775.jpg\"},{\"brief\":\"监测运动的减肥移动应用\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"南京市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4576,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"健康减肥4加1\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/201511/18/5c9f954ff4d2481e875cf4baab19be7e.jpg\"},{\"brief\":\"都市女性时尚购物指导平台\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"北京市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4461,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"薄荷女人网\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/201511/18/e00cc9a741844358a0df60bf15572af5.jpg\"},{\"brief\":\"健康安全检测服务平台\",\"phase\":\"未融资\",\"isFunding\":0,\"city\":\"深圳市\",\"hotTrend\":0,\"iosAlexa\":-1,\"andriodDownloads\":0,\"haveBp\":false,\"iosCate\":\"\",\"weChatWeeklyReading\":0,\"ccid\":4133,\"hotIncrement\":0,\"bpUrl\":\"\",\"name\":\"聚测无忧\",\"logo\":\"https://krplus-pic.b0.upaiyun.com/com_logo/%E8%81%9A%E6%B5%8B%E6%97%A0%E5%BF%A7.jpg\"}],\"totalCount\":9602},\"msg\":\"操作成功！\"}", count));
        printlnInfo(count.getTotalPages());*/
    }
}
